package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.SaleDAO;
import cn.edu.scau.cmi.hombio.dao.SalePigDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.Sale;
import cn.edu.scau.cmi.hombio.domain.SalePig;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for SalePig entities
 * 
 */

@Service("SalePigService")

@Transactional
public class SalePigServiceImpl implements SalePigService {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages Sale entities
	 * 
	 */
	@Autowired
	private SaleDAO saleDAO;

	/**
	 * DAO injected by Spring that manages SalePig entities
	 * 
	 */
	@Autowired
	private SalePigDAO salePigDAO;

	/**
	 * Instantiates a new SalePigServiceImpl.
	 *
	 */
	public SalePigServiceImpl() {
	}

	/**
	 * Return a count of all SalePig entity
	 * 
	 */
	@Transactional
	public Integer countSalePigs() {
		return ((Long) salePigDAO.createQuerySingleResult("select count(o) from SalePig o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Pig entity
	 * 
	 */
	@Transactional
	public SalePig saveSalePigPig(Integer id, Pig related_pig) {
		SalePig salepig = salePigDAO.findSalePigByPrimaryKey(id, -1, -1);
		Pig existingpig = pigDAO.findPigByPrimaryKey(related_pig.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpig != null) {
			existingpig.setId(related_pig.getId());
			existingpig.setNumber(related_pig.getNumber());
			existingpig.setEarNumber(related_pig.getEarNumber());
			existingpig.setRfid(related_pig.getRfid());
			existingpig.setBirthday(related_pig.getBirthday());
			existingpig.setState(related_pig.getState());
			existingpig.setMemo(related_pig.getMemo());
			related_pig = existingpig;
		}

		salepig.setPig(related_pig);
		related_pig.getSalePigs().add(salepig);
		salepig = salePigDAO.store(salepig);
		salePigDAO.flush();

		related_pig = pigDAO.store(related_pig);
		pigDAO.flush();

		return salepig;
	}

	/**
	 */
	@Transactional
	public SalePig findSalePigByPrimaryKey(Integer id) {
		return salePigDAO.findSalePigByPrimaryKey(id);
	}

	/**
	 * Delete an existing Sale entity
	 * 
	 */
	@Transactional
	public SalePig deleteSalePigSale(Integer salepig_id, Integer related_sale_id) {
		SalePig salepig = salePigDAO.findSalePigByPrimaryKey(salepig_id, -1, -1);
		Sale related_sale = saleDAO.findSaleByPrimaryKey(related_sale_id, -1, -1);

		salepig.setSale(null);
		related_sale.getSalePigs().remove(salepig);
		salepig = salePigDAO.store(salepig);
		salePigDAO.flush();

		related_sale = saleDAO.store(related_sale);
		saleDAO.flush();

		saleDAO.remove(related_sale);
		saleDAO.flush();

		return salepig;
	}

	/**
	 * Delete an existing SalePig entity
	 * 
	 */
	@Transactional
	public void deleteSalePig(SalePig salepig) {
		salePigDAO.remove(salepig);
		salePigDAO.flush();
	}

	/**
	 * Delete an existing Pig entity
	 * 
	 */
	@Transactional
	public SalePig deleteSalePigPig(Integer salepig_id, Integer related_pig_id) {
		SalePig salepig = salePigDAO.findSalePigByPrimaryKey(salepig_id, -1, -1);
		Pig related_pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		salepig.setPig(null);
		related_pig.getSalePigs().remove(salepig);
		salepig = salePigDAO.store(salepig);
		salePigDAO.flush();

		related_pig = pigDAO.store(related_pig);
		pigDAO.flush();

		pigDAO.remove(related_pig);
		pigDAO.flush();

		return salepig;
	}

	/**
	 * Return all SalePig entity
	 * 
	 */
	@Transactional
	public List<SalePig> findAllSalePigs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<SalePig>(salePigDAO.findAllSalePigs(startResult, maxRows));
	}

	/**
	 * Save an existing SalePig entity
	 * 
	 */
	@Transactional
	public void saveSalePig(SalePig salepig) {
		SalePig existingSalePig = salePigDAO.findSalePigByPrimaryKey(salepig.getId());

		if (existingSalePig != null) {
			if (existingSalePig != salepig) {
				existingSalePig.setId(salepig.getId());
				existingSalePig.setTime(salepig.getTime());
				existingSalePig.setMemo(salepig.getMemo());
			}
			salepig = salePigDAO.store(existingSalePig);
		} else {
			salepig = salePigDAO.store(salepig);
		}
		salePigDAO.flush();
	}

	/**
	 * Load an existing SalePig entity
	 * 
	 */
	@Transactional
	public Set<SalePig> loadSalePigs() {
		return salePigDAO.findAllSalePigs();
	}

	/**
	 * Save an existing Sale entity
	 * 
	 */
	@Transactional
	public SalePig saveSalePigSale(Integer id, Sale related_sale) {
		SalePig salepig = salePigDAO.findSalePigByPrimaryKey(id, -1, -1);
		Sale existingsale = saleDAO.findSaleByPrimaryKey(related_sale.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsale != null) {
			existingsale.setId(related_sale.getId());
			existingsale.setSaleType(related_sale.getSaleType());
			existingsale.setMemo(related_sale.getMemo());
			related_sale = existingsale;
		}

		salepig.setSale(related_sale);
		related_sale.getSalePigs().add(salepig);
		salepig = salePigDAO.store(salepig);
		salePigDAO.flush();

		related_sale = saleDAO.store(related_sale);
		saleDAO.flush();

		return salepig;
	}
}
